<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script typet="text/javascript" src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
    <script>
       /*  $.ajax({
			url:'http://171.221.244.102:7557/devices',
			success:function(res){
               
				$.each(res,function(){    
                    if(this['summary.serialNumber']=='5d5a4d1c75d7a'){
                        console.log(this)
					    console.log(new Date(this._lastInform).toLocaleString());
                    }         
				});
            }
        }); */

    var listen=(function(){
        var call;
        var orginalProto = Array.prototype;
        var arrayProto = Object.create(orginalProto); // 先克隆一份Array的原型出来
        var methodsToPatch = ['push','pop','shift','unshift','splice','sort','reverse'];
        methodsToPatch.forEach(function(v) {
            arrayProto[v] = function () {      
                orginalProto[v].apply(this, arguments);// 执行原始操作
                call({obj:this,mothod:v});
                observer(this);
            }
        });
       
        function defineProperty(obj, key, val){
            observer(val);
            Object.defineProperty(obj, key, {
                enumerable: true,
                configurable: true,
                get() { //读取方法
                    console.log('读取', key, '成功')
                    return val;
                },
                set(newval){ //赋值监听方法                                                                         
                    if (newval === val)return;
                    observer(newval);
                    console.log('监听赋值成功', newval)
                    val = newval; //可以执行渲染操作
                    call({obj:obj,key,key,value:newval});
                }
            })
        }

        function observer(obj) {
            var type=Object.prototype.toString.call(obj).replace(/\[object |]/g, '').toLowerCase();
            if ((type!='array' && type!='object') || obj == null || obj == undefined)return;
            if (type=='array') { 
                obj.__proto__ = arrayProto;//如果是数组, 重写原型  
                for (var k = 0; k < obj.length; k++){//传入的数据可能是多维度的,也需要执行响应式
                    defineProperty(obj, k, obj[k])
                }
            } else {
                for (var k in obj)defineProperty(obj, k, obj[k]) // 给对象中的每一个方法都设置响应式
            }
        } 
        
        return function(obj,callback){
            call=callback;
            observer(obj)
        }
    }());



    var a=[];
    listen(a,function(a,b,c){
        console.log(arguments)
    }) 
   


    </script>
    <!-- <script src="test.js"></script> -->
</body>
</html>